package me.jiatao.redpacket;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

import me.jiatao.redpacket.util.AccessbilityUtil;
import me.jiatao.redpacket.util.SPDataUtils;
import me.jiatao.redpacket.util.ScreenUtil;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.Toolbar;

import me.jiatao.redpacket.service.RedpacketService;

public class MainActivity extends AppCompatActivity {

    SwitchCompat swWx;
    CheckBox ckSingle, ckSelf, ckPause, ckFail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ScreenUtil.getScreenSize(this);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ckSingle = findViewById(R.id.ckSingle);
        ckSelf = findViewById(R.id.ckSelf);
        ckPause = findViewById(R.id.ckPause);
        ckFail = findViewById(R.id.ckFail);

        swWx = findViewById(R.id.swWx);
        swWx.setOnClickListener((v) -> {
            startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
        });

        //ckFail.setOnCheckedChangeListener((compoundButton, b) -> RedpacketService.isCloseFail = b);
        ckSingle.setOnCheckedChangeListener((compoundButton, b) -> RedpacketService.isSingle = b);
        ckPause.setOnCheckedChangeListener((compoundButton, b) -> RedpacketService.isPause = b);

        initSeekBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.app_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_about) {
            AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                    .setIcon(R.mipmap.icon)
                    .setTitle("关于")
                    .setMessage("作者：贾涛" +
                            "\n本程序适配 [钉钉 version6.0.2]" +
                            "\n\n仅供娱乐，请勿商用\n一切后果，概不承担" +
                            "\n\n谢谢使用!")
                    .setNegativeButton("关闭", (dialogInterface, which) -> {
                        dialogInterface.dismiss();
                    })
                    .create();
            dialog.show();
        }
        return false;
    }

    private void initSeekBar() {
        /**
         * 设置红包开启时间
         */
        int openTime = SPDataUtils.getInstance().getInt(SPDataUtils.OPEN_PACKET_TIME_KEY);

        TextView tvOpenPacketTime = findViewById(R.id.tv_open_packet_time);
        tvOpenPacketTime.setText(openTime + "秒");
        SeekBar sb_open_packet = findViewById(R.id.sb_open_packet);
        sb_open_packet.setProgress(openTime);
        sb_open_packet.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvOpenPacketTime.setText(progress + "秒");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                SPDataUtils.getInstance().saveInt(SPDataUtils.OPEN_PACKET_TIME_KEY, seekBar.getProgress());
            }
        });

        /**
         * 设置关闭红包界面时间
         */

        int closeTime = SPDataUtils.getInstance().getInt(SPDataUtils.CLOSE_PACKET_VIEW_TIME_KEY);

        TextView tvCloseResultTime = findViewById(R.id.tv_close_result_time);
        //读取关闭红包界面默认值
        tvCloseResultTime.setText(closeTime + "秒");
        SeekBar sb_close_result = findViewById(R.id.sb_close_result);
        sb_close_result.setProgress(closeTime);
        sb_close_result.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvCloseResultTime.setText(progress + "秒");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                SPDataUtils.getInstance().saveInt(SPDataUtils.CLOSE_PACKET_VIEW_TIME_KEY, seekBar.getProgress());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        swWx.setChecked(AccessbilityUtil.isAccessibilitySettingsOn(this, RedpacketService.class));
    }

    public void clearPosition(View view) {
        RedpacketService.viewRects.clear();
    }
}
